<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link rel="stylesheet" href="../css/wzl-bootstrap.css" />
    <title>积分转赠</title>
    <style>

    </style>
</head>
<body>
<div>
    <nav class="wzl-navbar navbar-default" role="navigation" >
        <div class="container-fluid">
            <div class="navbar-header">
                <div class="wzl-nav-bar" onclick="history.back(-1)">
                    <span class="glyphicon glyphicon-wzl-back" ></span>积分转赠
                </div>
            </div>
        </div>
    </nav>
</div>
<div class="">
    <div class="pd-line">可用积分(<span class="red" id="has_points">-</span>分)</div>
    <div class="pd-line">
        <span>对方手机号:</span>
        <input class="in-amount" id="toPhoneNum" type="number" placeholder="请输入接受您赠送的手机号">
    </div>
    <div class="pd-line">
        <span>赠送积分:</span>
        <input class="in-amount" id="points" type="number" placeholder="请输入整数积分">
    </div>
    <!--<div class="line-tips">
        <span class="red">提示：</span>
        <span>兑换比例1:10，例 1元=10积分</span>
    </div>-->
    <div class="submit-btn" onclick="givepoints()">赠送</div>
</div>



<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="../../js/common.js"></script>
<script src="https://cdn.bootcss.com/layer/3.0.1/layer.min.js"></script>

<script type="application/javascript">
    var has_points;
    $(function () {
        initPoint();

    })
    //积分转赠
    function givepoints() {
        var reg = /^[0-9]*[1-9][0-9]*$/;
        //手机号正则
        var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
        var toPhoneNum = $("#toPhoneNum").val();
        if(!toPhoneNum){
            layer.msg("请输入对方手机号！");
            return;
        }
        if(!phoneReg.test(toPhoneNum)){
            layer.msg("请输入正确的手机号");
            return;
        }
        var points = $("#points").val();
        if(!points){
            layer.msg("请输入要赠送的积分！");
            return;
        }
        if(!reg.test(points)){
            layer.msg("请输入整数积分");
            return;
        }
        points = parseFloat(points);
        has_points = parseFloat(has_points);
        if(points<=0){
            layer.msg("请输入要赠送的积分！");
            return;
        }
        if(points>has_points){
            layer.msg("可用积分不足！");
            return;
        }
        layer.confirm('是否赠送?', {icon: 3, title:'提示'}, function(index){
            var phoneNum = window.localStorage.getItem("phoneNum");
            var paramStr = {};
            paramStr.phoneNum =phoneNum;
            paramStr.toPhoneNum =toPhoneNum;
            paramStr.points = points;
            var params = {};
            params.paramStr = JSON.stringify(paramStr);
            Common.ajaxWithParamBack("/fenful/api/v1/account/transferPoints",params,function (obj) {
                if(obj.code=="200"){
                    layer.msg("赠送成功！");
                    initPoint();
                }else{
                    layer.msg(obj.message);
                }
                layer.close(index);
            });

        });

    }

    function initPoint() {
        var token=localStorage.getItem("token");
        var phoneNum=localStorage.getItem("phoneNum");
        var paramStr = {};
        paramStr.phoneNum =phoneNum;
        var params = {};
        params.paramStr = JSON.stringify(paramStr);
        $.ajax({
            headers: {"phone":phoneNum,"token":token},
            type: "POST",
            data:params,
            url:'/fenful/api/v1/user/getUser',
            datatype: "json",
            async: false,
            success: function(re) {

                var data=JSON.parse(re.data);
                has_points = data.points;
                $("#has_points").text(data.points);
            },
            error: function(re) {

            }
        })

    }
</script>
</body>
</html>